---
title: "Policy Outcome Results Markdown"
author: "G. Agustin Markarian"
date: "2023-10-27"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(message = FALSE)
knitr::opts_chunk$set(results =  'asis')
knitr::opts_chunk$set(table.placement = "H")

library(stargazer)
library(readr)
library(fixest)
library(tidyverse)
library(lfe)
library(usmap)
library(ggplot2)
library(gridExtra)
library(scales)
library(dotwhisker)
library(cdlTools)
library(cowplot)
library(data.table)
library(sjPlot)
library(sjmisc)
library(gridExtra)
library(grid)
'%!in%' <- function(x,y)!('%in%'(x,y))

directory.location <- rprojroot::find_root(rprojroot::is_rstudio_project)
knitr::opts_knit$set(root.dir = file.path(directory.location))

```

## R Markdown

```{r}
policy <- fread("Data/policy_outcomes_withissueareas_AM25.csv") %>%
  rename(vote_before_cces = vote_before_cces.x) %>%
  subset(outcome <= 1 & !is.na(outcome))
```

## Including Plots

```{r}
policy$district <- paste(policy$cd, policy$state.y, sep = "_")

policy$white_r <- policy$race=="1"
policy$black_r <- policy$race=="2"
policy$latino_r <- policy$race=="3"
policy$asian_r <- policy$race=="4"
policy$other_r <- policy$race%in%c("5", "6", "7", "8", "9")


policy$first_quartile_r <- policy$income=="1"
policy$second_quartile_r <- policy$income=="2"
policy$third_quartile_r <- policy$income=="3"
policy$fourth_quartile_r <- policy$income=="4"

policy$highschool_r <- policy$edu %in% c("1", "2")
policy$somecollege_r <- policy$edu %in% c("3", "4")
policy$bachelors_r <- policy$edu %in% c("5")
policy$postgrad_r <- policy$edu %in% c("6")

policy$became_law <- ifelse(is.na(policy$became_law) , 0, policy$became_law)
policy$passed_house <- ifelse(is.na(policy$passed_house) , 0, policy$passed_house)
policy$passed_senate <- ifelse(is.na(policy$passed_senate) , 0, policy$passed_senate)

policy$pref_outcome <- ifelse(policy$outcome==policy$became_law , 1, 0)
policy$house_outcome <- ifelse(policy$outcome==policy$passed_house , 1, 0)
policy$senate_outcome <- ifelse(policy$outcome==policy$passed_senate , 1, 0)


#Turning 99s and 98s into NAs
policy <- policy %>%
  mutate(pid7 = ifelse(pid7>7, NA, pid7),
         ideo = ifelse(ideo>5, NA, ideo),
         edu = ifelse(edu>6, NA, edu),
         income = ifelse(income>4, NA, income),
         reg_voter = ifelse(reg_voter>1, NA, reg_voter),
         age =  as.numeric(year) - as.numeric(dob), 
         female = ifelse(female==1,1,ifelse(female==0,0, NA)),
         year_par = ifelse(!is.na(year.y), year.y, year))


policy$reppres <- ifelse(policy$year_par %!in% c(2009:2016, 2021:2022), 1, 0)
policy$rephouse <- ifelse(policy$year_par %in%c(200:2006, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018), 1, 0)
policy$repsenate <- ifelse(policy$year_par %in%c(2003:2006, 2015, 2016, 2017, 2018, 2019, 2020), 1, 0)

# Removing nominations
nominations <- c("2017_rcv8_r", "2017_rcv2_r" ,"2009_rcv7_r" ,"2018_rcv4_r" ,"2019_rcv3_r" ,"2022_rcv7_r" ,"2020_rcv3_r" ,"2018_rcv1_r" ,"2016_rcv1_r")

policy <- filter(policy, year_vote %!in% nominations)
```



# Main Models

## Figure 1

```{r}
economics <- c(1, 3:8, 10, 13, 14 , 15, 17, 18, 21)
cultural <- c(2, 9, 12)
defense <- c(16, 19)
other <- c(20)

opinion <- policy %>%
  subset(race <= 4) %>%
  mutate(pid3 = ifelse(pid7 <=3, "Democrat", 
                       ifelse(pid7==4, "Independent", "Republican")),
         race = ifelse(race == 1, "White Respondent", 
                       ifelse(race==2, "Black Respondent", 
                              ifelse(race==3, "Latino Respondent", "Asian Respondent")))
         ) %>%
  group_by(race, pid3, cces_name.x) %>%
  summarize(pref = weighted.mean(outcome, weight, na.rm=T),
            upper_pref = pref + (sd(outcome)/n()),
            lower_pref = pref - (sd(outcome)/n())) %>%
  subset(!is.na(pid3)) %>%
  unite("racepid", c(race, pid3), remove = FALSE) 

opinion2 <- opinion %>%
  subset(cces_name.x %in% c( "Minnimum Wage", "Bank Bailout",  "Extend NAFTA", "Stem Cell Research", "Amendment to Ban Gay Marriage", "Illegal Immigrant Citizenship", "Renew Patriot Act phone survielence", "Iran sanctions act", "Iraq Withdrawal")) %>% 
  subset(pid3 != "Independent") %>%
  mutate(area = recode(cces_name.x,  "Minnimum Wage"="Economic", "Bank Bailout"="Economic", "Extend NAFTA"="Economic", "Stem Cell Research"="Cultural", "Amendment to Ban Gay Marriage"="Cultural", "Illegal Immigrant Citizenship"="Cultural", "Renew Patriot Act phone survielence"="Defense", "Iran sanctions act"="Defense", "Iraq Withdrawal"="Defense"),
         area = factor(area, levels = c("Economic", "Cultural","Defense")), 
         cces_name.x = recode(cces_name.x,  "Minnimum Wage"="Minimum Wage", "Bank Bailout"="Bank Bailout (TARP)", "Extend NAFTA"="Extend NAFTA", "Stem Cell Research"="Stem Cell Research", "Amendment to Ban Gay Marriage"="Ban Gay Marriage", "Illegal Immigrant Citizenship"="Pathway to Citizenship for\nUndocumented Immigrants", "Renew Patriot Act phone survielence"="Renew Phone Surveillance\n(Patriot Act)", "Iran sanctions act"="Iran Sanctions Act", "Iraq Withdrawal"= "Withdraw Troops from Iraq"))
```

```{r fig.height=6, fig.width=6}
ggplot(opinion2, aes(x=pref, y=cces_name.x, color = pid3, shape = race)) +
  geom_point(position = position_dodge(width = .4), size = 2)  +  
  labs(y = "", x = "Share in Support",  shape = "Race") +
  scale_color_manual(labels = c("Democrat",  "Republican"), values = c("navyblue", "lightpink2"), name = "Party ID") +
    theme_bw() +
  theme(legend.position = "bottom",
    legend.box = "vertical",
    legend.title = element_text(size = 9), 
    legend.text = element_text(size = 9),
    legend.box.spacing =  unit(0, 'cm')) +
  facet_wrap(vars(area), scales = 'free', nrow = 3) +
  guides(
    color = guide_legend(order = 1, nrow = 1),  # Color legend in first row
    shape = guide_legend(order = 2, nrow = 2)   # Shape legend in second row
  )
```


## Figure 3
```{r}
#Race Simple Models with Controls
m1 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           |  as.factor(year_vote) | 0 |  year_vote, data = policy, weights = policy$weight)
```

```{r}
stargazer(m1, 
          title = "Table SI-G1",
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law"),
          out = "Tables/Table SI-G1.doc",
          no.space = T
)
```

```{r}
element_custom <- function() {
  structure(list(), class = c("element_custom", "element_text"))
}

element_grob.element_custom <- function(element, label="", ...)  {
  disect <- strsplit(label, "\\n")[[1]]
  labels <- lapply(disect, function(x) tryCatch(parse(text=x), 
                                                error = function(e) x))
  hl <-  unit(rep(1, length(labels)), 'strheight', data=labels) + unit(0.1,"line")
  yl <- c(list(unit(0,"line")), 
          lapply(seq_along(labels[-length(labels)]), function(ii) sum(hl[1:ii])))

  cl <- do.call(gList, Map(function(label, ii) 
    textGrob(label, y = unit(1,"npc")-yl[[ii]], hjust=0, x=0, vjust=1), 
    label = labels, ii = seq_along(labels)))

  gTree(children = cl, cl="sl", heights = hl, gp=gpar(col="grey50",fontsize=8))
}

heightDetails.sl <- function(x) sum(x$heights)
```


```{r}
#Plot
m1df <- broom::tidy(m1) %>% 
  mutate(model = "Model 1") %>%
  subset(!grepl("other",term) & !is.nan(estimate))

models1 <- rbind(m1df) %>%
  relabel_predictors(black_rTRUE = "Black Respondent",
                     latino_rTRUE = "Latino Respondent",
                     asian_rTRUE = "Asian Respondent")


dwplot(models1, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       )) +
    theme_bw(base_size = 11) + xlab("") + ylab("") +

  labs(x="Coefficient Estimate", caption = "Notes:\n
  1) White Respondents are the reference category (White average=0.495).\n
  2) Individuals with other racial self-categorizations are excluded from the figure.\n     
  3) All models include policy question fixed effects and standard errors clustered at that level.") +
    scale_x_continuous(limits = c(-0.1,0.1)) +
  (theme_bw() %+replace% theme(plot.caption = element_custom())) +    
  theme(legend.position = "none",
        plot.title = element_text(face = "bold")) +
  scale_color_grey() +
    annotate(geom = "text", x = 0.005, y= 2, label = "White Respondent", color = "grey40",
             angle = 90, size = 3.5) 
```

### DV weighted.means

```{r}
weighted.mean(subset(policy, white_r ==1 & reppres==1)$pref_outcome, subset(policy, white_r ==1 & reppres==1)$weight, na.rm=T)
sd(subset(policy, white_r ==1 & reppres==1)$pref_outcome)
weighted.mean(subset(policy, white_r ==1 & rephouse==1)$pref_outcome, subset(policy, white_r ==1 & rephouse==1)$weight, na.rm=T)
sd(subset(policy, white_r ==1 & rephouse==1)$pref_outcome, na.rm=T)
weighted.mean(subset(policy, white_r ==1 & repsenate==1)$pref_outcome, subset(policy, white_r ==1 & repsenate==1)$weight, na.rm=T)
sd(subset(policy, white_r ==1 & repsenate==1)$pref_outcome, na.rm=T)

weighted.mean(subset(policy, white_r ==1 & reppres==0)$pref_outcome, subset(policy, white_r ==1 & reppres==0)$weight, na.rm=T)
sd(subset(policy, white_r ==1 & reppres==0)$pref_outcome)
weighted.mean(subset(policy, white_r ==1 & rephouse==0)$pref_outcome, subset(policy, white_r ==1 & rephouse==0)$weight, na.rm=T)
sd(subset(policy, white_r ==1 & rephouse==0)$pref_outcome, na.rm=T)
weighted.mean(subset(policy, white_r ==1 & repsenate==0)$pref_outcome, subset(policy, white_r ==1 & repsenate==0)$weight, na.rm=T)
sd(subset(policy, white_r ==1 & repsenate==0)$pref_outcome, na.rm=T)
```



## Figure 4

```{r}
g <- policy %>%
  subset(race <= 4) %>%
  group_by(year_par, race, reppres, repsenate, rephouse) %>%
  summarise(pref_outcome = weighted.mean(pref_outcome, weight, na.rm=T)) %>%
  mutate(race = dplyr::recode(race, "1"="white", "2"="black", "3"="latino", "4"="asian")) %>%
  pivot_wider(names_from = "race", values_from = pref_outcome) %>%
  mutate(Black_Americans = black-white,
         Latinos = latino-white,
         Asian_Americans = asian-white) %>%
  pivot_longer(c(Black_Americans:Asian_Americans)) %>%
  mutate(congress = ifelse(repsenate == 1 & rephouse == 1, "Republican", 
                           ifelse(repsenate ==0 & rephouse == 0, "Democrat", "Divided")))

```


```{r}
ggplot(subset(g, year_par >= 2006), aes(x = year_par, y = value, color = as.factor(reppres), shape = congress)) +
  geom_point() +
  geom_hline(yintercept = 0, color = "red", linetype = 'dotted') +
  theme_bw() +
  scale_color_manual(labels = c("Democrat", "Republican"), values = c("navyblue", "lightpink2"), name = "Presidential Control") + 
  labs(shape="Congressional Control", y = "Representational Gap Relative to White Americans", x = "Year") +
  facet_wrap(vars(name), nrow = 3) +
  scale_y_continuous(limits = c(-.24, .18))
```


## Figure 5

```{r}
m1.1 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, majortopic %in% economics),
           weights = subset(policy, majortopic %in% economics)$weight)

m1.2 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, majortopic %in% cultural),
           weights = subset(policy, majortopic %in% cultural)$weight)

m1.3 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, majortopic %in% defense),
           weights = subset(policy, majortopic %in% defense)$weight)

m1.4 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, majortopic %in% other),
           weights = subset(policy, majortopic %in% other)$weight)
```

```{r}
stargazer(m1.1, m1.2, m1.3, m1.4,
          title = "Table SI-G2",
          #omit= c("vote", "year"), 
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law"),
          column.labels = c("Economic Policy", "Cultural Policy", "Defense Policy", "Goverment Operations"),
          add.lines=list(c("White Respondent Mean", "0.501", "0.373", "0.667", "0.498")),
          out = "Tables/Table SI-G2.doc",
          no.space = T
)
```

```{r}
poliss <- policy  %>%
  subset(race <= 4) %>%
  mutate(race = dplyr::recode(race, "1" = "white", "2" = "black", "3" = "latino", "4" = "asian")) %>%
  group_by(year_vote, race) %>%
  summarise(support = weighted.mean(outcome, weight, na.rm=T)) %>%
  pivot_wider(names_from = race, values_from = support) %>%
  mutate(wb_gap = abs(white - black),
         wl_gap = abs(white - latino),
         wa_gap = abs(white - asian))
```


```{r}
m2.1 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, year_vote %in% subset(poliss, wb_gap > median(poliss$wb_gap))$year_vote),
           weights = subset(policy, year_vote %in% subset(poliss, wb_gap > median(poliss$wb_gap))$year_vote)$weight)

m2.2 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, year_vote %in% subset(poliss, wl_gap > median(poliss$wl_gap))$year_vote),
           weights = subset(policy, year_vote %in% subset(poliss, wl_gap > median(poliss$wl_gap))$year_vote)$weight)

m2.3 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, year_vote %in% subset(poliss, wa_gap > median(poliss$wa_gap))$year_vote),
           weights = subset(policy, year_vote %in% subset(poliss, wa_gap > median(poliss$wa_gap))$year_vote)$weight)

```

```{r}
stargazer(m2.1, m2.2, m2.3, 
          title = "Table SI-G3",
          #omit= c("vote", "year"), 
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law"),
          column.labels = c("Larger White-Black Gap", "Larger White-Latino Gap", "Larger White-Asian Gap"),
          add.lines=list(c("White Respondent Mean", "0.493", "0.475", "0.490")),
           out = "Tables/Table SI-G3.doc",
          no.space = T
)
```

```{r fig.height=5, fig.width=6.5}
#Plot
m1df <- broom::tidy(m1.1) %>% 
  mutate(model = "Economic Policy") %>%
  subset(!grepl("other",term) & !is.nan(estimate))

m2df <- broom::tidy(m1.2) %>% 
  mutate(model = "Cultural Policy") %>%
  subset(!grepl("other",term) & !is.nan(estimate))

m3df <- broom::tidy(m1.3) %>% 
  mutate(model = "Defense Policy") %>%
  subset(!grepl("other",term) & !is.nan(estimate))

m4df <- broom::tidy(m1.4) %>% 
  mutate(model = "Government Operations") %>%
  subset(!grepl("other",term) & !is.nan(estimate))


models1 <- rbind(m1df, m2df, m3df, m4df) %>%
  relabel_predictors(black_rTRUE = "Black Respondent",
                     latino_rTRUE = "Latino Respondent",
                     asian_rTRUE = "Asian Respondent")


g1 <- dwplot(models1, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ), dot_args = list(aes(shape = model))) +
    theme_bw(base_size = 11) + xlab("") + ylab("") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    labs(subtitle = "5.1 Issue Area") +
    scale_x_continuous(limits = c(-0.27,0.25)) +
    guides(
        shape = guide_legend("", nrow = 4), 
        colour = guide_legend("", nrow = 4)) +
        theme(legend.direction="horizontal") + scale_color_grey()

m1df <- broom::tidy(m2.1) %>% 
  mutate(model = "Larger White-Black Gap") %>%
  subset(!grepl("other",term) & !is.nan(estimate))

m2df <- broom::tidy(m2.2) %>% 
  mutate(model = "Larger White-Latino Gap") %>%
  subset(!grepl("other",term) & !is.nan(estimate))

m3df <- broom::tidy(m2.3) %>% 
  mutate(model = "Larger White-Asian Gap") %>%
  subset(!grepl("other",term) & !is.nan(estimate))


models2 <- rbind(m1df, m2df, m3df) %>%
  relabel_predictors(black_rTRUE = "Black Respondent",
                     latino_rTRUE = "Latino Respondent",
                     asian_rTRUE = "Asian Respondent")

g2 <- dwplot(models2, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ), dot_args = list(aes(shape = model))) +
    theme_bw(base_size = 11) + xlab("") + ylab("") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    labs(subtitle = "5.2 Racially Polarized Issues") +
    scale_x_continuous(limits = c(-0.27,0.25)) +
    guides(
        shape = guide_legend("", nrow = 3), 
        colour = guide_legend("", nrow = 3)) +
        theme(legend.direction="horizontal", axis.text.y = element_blank()) + scale_color_grey()

g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}


mylegend1 <- g_legend(g1)

g1 <- g1 + theme(legend.position="none")

mylegend2 <- g_legend(g2)

g2 <- g2 + theme(legend.position="none")

lay = rbind(c(1,1,1,1,1,1,2,2,2,2),
            c(1,1,1,1,1,1,2,2,2,2),
            c(1,1,1,1,1,1,2,2,2,2),
            c(1,1,1,1,1,1,2,2,2,2),
            c(1,1,1,1,1,1,2,2,2,2),
            c(1,1,1,1,1,1,2,2,2,2),
            c(1,1,1,1,1,1,2,2,2,2),
            c(1,1,1,1,1,1,2,2,2,2),
            c(1,1,1,1,1,1,2,2,2,2),
            c(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA),
            c(NA,NA,3,3,3,NA,4,4,4,4),
            c(NA,NA,3,3,3,NA,4,4,4,4),
            c(NA,NA,3,3,3,NA,4,4,4,4))




p3 <- grid.arrange(g1,g2,mylegend1, mylegend2, layout_matrix=lay)

#p3 + grid.text("Coefficient Estimate", 
#          x=unit(0.6, "npc"), 
#          y=unit(.3, "npc"),
#          gp=gpar(fontsize=10))
```


## Figure 6

```{r}
m1 <- felm(pref_outcome ~ black_r*reppres + latino_r*reppres + asian_r*reppres + other_r*reppres
           |  as.factor(year_vote) | 0 |  year_vote, data = policy, weights = policy$weight)

m2 <- felm(pref_outcome ~ black_r*rephouse + latino_r*rephouse + asian_r*rephouse + other_r*rephouse 
           | as.factor(year_vote) | 0 |  year_vote, data = policy, weights = policy$weight)

m3 <- felm(pref_outcome ~ black_r*repsenate + latino_r*repsenate + asian_r*repsenate + other_r*repsenate
           |  as.factor(year_vote) | 0 |  year_vote, data = policy, weights = policy$weight)
```

```{r}
stargazer(m1, m2 , m3,
          title = "Table SI-G4",
          #omit= c("vote", "year"), 
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law"),
          out = "Tables/Table SI-G4.doc",
          no.space = T
)
```


```{r}
#Plot
m1df <- broom::tidy(m1) %>% 
  mutate(model = "Model 1") %>%
  subset(!grepl("other",term) & !is.nan(estimate))

m2df <- broom::tidy(m2) %>% 
  mutate(model = "Model 2") %>%
  subset(!grepl("other",term) & !is.nan(estimate))

m3df <- broom::tidy(m3) %>% 
  mutate(model = "Model 3") %>%
  subset(!grepl("other",term) & !is.nan(estimate))



models1 <- rbind(m1df) %>%
  relabel_predictors(black_rTRUE = "Black Respondent*Democratic\nControl of Veto Point",
                     latino_rTRUE = "Latino Respondent*Democratic\nControl of Veto Point",
                     asian_rTRUE = "Asian Respondent*Democratic\nControl of Veto Point",
                     "black_rTRUE:reppres" = "Black Respondent*Republican\nControl of Veto Point",
                     "reppres:latino_rTRUE" = "Latino Respondent*Republican\nControl of Veto Point",
                     "reppres:asian_rTRUE" = "Asian Respondent*Republican\nControl of Veto Point")

models2 <- rbind(m2df) %>%
  relabel_predictors(black_rTRUE = "Black Respondent*Democratic\nControl of Veto Point",
                     latino_rTRUE = "Latino Respondent*Democratic\nControl of Veto Point",
                     asian_rTRUE = "Asian Respondent*Democratic\nControl of Veto Point",
                     "black_rTRUE:reppres" = "Black Respondent*Republican\nControl of Veto Point",
                     "reppres:latino_rTRUE" = "Latino Respondent*Republican\nControl of Veto Point",
                     "reppres:asian_rTRUE" = "Asian Respondent*Republican\nControl of Veto Point")

models3 <- rbind(m3df) %>%
  relabel_predictors(black_rTRUE = "Black Respondent*Democratic\nControl of Veto Point",
                     latino_rTRUE = "Latino Respondent*Democratic\nControl of Veto Point",
                     asian_rTRUE = "Asian Respondent*Democratic\nControl of Veto Point",
                     "black_rTRUE:reppres" = "Black Respondent*Republican\nControl of Veto Point",
                     "reppres:latino_rTRUE" = "Latino Respondent*Republican\nControl of Veto Point",
                     "reppres:asian_rTRUE" = "Asian Respondent*Republican\nControl of Veto Point")

g1 <- dwplot(models1, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ), dot_args = list(aes(shape = model))) +
    theme_bw(base_size = 11) + xlab("") + ylab("") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    labs(subtitle = "6.1 President") +
    scale_x_continuous(limits = c(-0.27,0.15)) +
    guides(
        shape = guide_legend(""), 
        colour = guide_legend("")) +
        theme(legend.direction="horizontal") + scale_color_grey()


g2 <- dwplot(models2, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ), dot_args = list(aes(shape = model))) +
    theme_bw(base_size = 11) + xlab("") + ylab("") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    labs(subtitle = "6.2 House") +
    scale_x_continuous(limits = c(-0.27,0.15)) +
    guides(
        shape = guide_legend(""), 
        colour = guide_legend("")) +
        theme(legend.position = "none", axis.text.y = element_blank()) + scale_color_grey()

g3 <- dwplot(models3, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ), dot_args = list(aes(shape = model))) +
    theme_bw(base_size = 11) + xlab("") + ylab("") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    labs(subtitle = "6.3 Senate") +
    scale_x_continuous(limits = c(-0.27,0.15)) +
    guides(
        shape = guide_legend(""), 
        colour = guide_legend("")) +
        theme(legend.position = "none", axis.text.y = element_blank()) + scale_color_grey()



mylegend<-g_legend(g1)

g1 <- g1 + theme(legend.position="none")

lay = rbind(c(1,1,2,3),
            c(1,1,2,3),
            c(1,1,2,3),
            c(1,1,2,3),
            c(1,1,2,3),
            c(1,1,2,3),
            c(1,1,2,3),
            c(1,1,2,3),
            c(1,1,2,3))




p3 <- grid.arrange(g1,g2,g3,layout_matrix=lay, bottom = "Coefficient Estimate")

p3
```

## Figure 7

```{r}
m1.1 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r + income + edu + female + age
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 1),
           weights = subset(policy, reppres == 1)$weight)

m1.2 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r + ideo
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 1),
           weights = subset(policy, reppres == 1)$weight)

m1.3 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r + pid7
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 1),
           weights = subset(policy, reppres == 1)$weight)

m1.4 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 1),
           weights = subset(policy, reppres == 1)$weight)
```

```{r}
stargazer(m1.4, m1.1, m1.2, m1.3,
          title = "Table SI-G5",
          #omit= c("vote", "year"), 
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law"),
          out = "Tables/Table SI-G5.doc"
)
```

```{r}
m2.1 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r + income + edu + female + age
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 0),
           weights = subset(policy, reppres == 0)$weight)

m2.2 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r + ideo
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 0),
           weights = subset(policy, reppres == 0)$weight)

m2.3 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r + pid7
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 0),
           weights = subset(policy, reppres == 0)$weight)

m2.4 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 0),
           weights = subset(policy, reppres == 0)$weight)
```

```{r}
stargazer(m2.4, m2.1, m2.2, m2.3,
          title = "Table SI-G6",
          #omit= c("vote", "year"), 
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law"),
          out = "Tables/Table SI-G6.doc"
)
```

```{r}
m3.1 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r + income + edu + female + age
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, repsenate == 1),
           weights = subset(policy, repsenate == 1)$weight)

m3.2 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r + ideo
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, repsenate == 1),
           weights = subset(policy, repsenate == 1)$weight)

m3.3 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r + pid7
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, repsenate == 1),
           weights = subset(policy, repsenate == 1)$weight)

m3.4 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, repsenate == 1),
           weights = subset(policy, repsenate == 1)$weight)
```

```{r}
stargazer(m3.4, m3.1, m3.2, m3.3,
          title = "Table SI-G7",
          #omit= c("vote", "year"), 
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law"),
          out = "Tables/Table SI-G7.doc"
)
```

```{r}
m4.1 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r + income + edu + female + age
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, repsenate == 0),
           weights = subset(policy, repsenate == 0)$weight)

m4.2 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r + ideo
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, repsenate == 0),
           weights = subset(policy, repsenate == 0)$weight)

m4.3 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r + pid7
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, repsenate == 0),
           weights = subset(policy, repsenate == 0)$weight)

m4.4 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, repsenate == 0),
           weights = subset(policy, repsenate == 0)$weight)
```

```{r}
stargazer(m4.4, m4.1, m4.2, m4.3,
          title = "Table SI-G8",
          #omit= c("vote", "year"), 
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law"),
          out = "Tables/Table SI-G8.doc"
)
```

```{r fig.height=8.5, fig.width=6.5}

m1df <- broom::tidy(m1.1) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE")) %>% 
  mutate(model = "Demo. Covariates")
  
m2df <- broom::tidy(m1.2) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "Ideology Covariate")

m3df <- broom::tidy(m1.3) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "PID Covariate")

m4df <- broom::tidy(m1.4) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "No Covariates")
  

models <- rbind(m4df, m1df, m2df, m3df) 

 
g1 <- dwplot(models, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ),  dot_args = list(aes(shape = model))) %>% # plot line at zero _behind_coefs
    relabel_predictors(
        c(  black_rTRUE = "Black",
            latino_rTRUE = "Latino",
            asian_rTRUE = "Asian"
        )) + # plot line at zero _behind_ coefs
    theme_bw(base_size = 11) + xlab("") + ylab("") +
    labs(subtitle = "7.1 Republican President") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    scale_colour_grey(
        start = .1,
        end = .7
    ) +
    scale_shape_discrete(
        name = "Model",
    ) +
    guides(
        shape = guide_legend("", nrow=1,byrow=TRUE), 
        colour = guide_legend("", nrow=1,byrow=TRUE)) +
        theme(legend.direction="horizontal") +
  scale_x_continuous(limits = c(-0.18, 0.14)) +
     theme(legend.position = "bottom") 

g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

mylegend<-g_legend(g1)

g1 <- g1 + theme(legend.position="none") + labs(x="")


m1df <- broom::tidy(m2.1) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE")) %>% 
  mutate(model = "Demo. Mediators")
  
m2df <- broom::tidy(m2.2) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "Ideology Mediator")

m3df <- broom::tidy(m2.3) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "PID Mediator")

m4df <- broom::tidy(m2.4) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "No Mediators")
  

models <- rbind(m4df, m1df, m2df, m3df) 

 
g2 <- dwplot(models, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ),  dot_args = list(aes(shape = model))) %>% # plot line at zero _behind_coefs
    relabel_predictors(
        c(  black_rTRUE = "Black",
            latino_rTRUE = "Latino",
            asian_rTRUE = "Asian"
        )) + # plot line at zero _behind_ coefs
    theme_bw(base_size = 11) + xlab("") + ylab("") +
    labs(subtitle = "7.2 Democratic President") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    scale_colour_grey(
        start = .1,
        end = .7
    ) +
    scale_shape_discrete(
        name = "Model",
    ) +
    guides(
        shape = guide_legend("Model"), 
        colour = guide_legend("Model")) +
        theme(legend.position = c(0.55, 0.01)
    ) + theme(legend.position="none") +
  scale_x_continuous(limits = c(-0.18, 0.14))


g2 <- g2 + theme(legend.position="none") + labs(x="")


m1df <- broom::tidy(m3.1) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE")) %>% 
  mutate(model = "Demo. Mediators")
  
m2df <- broom::tidy(m3.2) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "Ideology Mediator")

m3df <- broom::tidy(m3.3) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "PID Mediator")

m4df <- broom::tidy(m3.4) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "No Mediators")
  

models <- rbind(m4df, m1df, m2df, m3df) 

 
g3 <- dwplot(models, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ),  dot_args = list(aes(shape = model))) %>% # plot line at zero _behind_coefs
    relabel_predictors(
        c(  black_rTRUE = "Black",
            latino_rTRUE = "Latino",
            asian_rTRUE = "Asian"
        )) + # plot line at zero _behind_ coefs
    theme_bw(base_size = 11) + xlab("") + ylab("") +
    labs(subtitle = "7.3 Republican Senate") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    scale_colour_grey(
        start = .1,
        end = .7
    ) +
    scale_shape_discrete(
        name = "Model",
    ) +
    guides(
        shape = guide_legend(""), 
        colour = guide_legend("")) +
        theme(legend.direction="horizontal") +
  scale_x_continuous(limits = c(-0.18, 0.14))

g3 <- g3 + theme(legend.position="none") + labs(x="")


m1df <- broom::tidy(m4.1) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE")) %>% 
  mutate(model = "Demo. Mediators")
  
m2df <- broom::tidy(m4.2) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "Ideology Mediator")

m3df <- broom::tidy(m4.3) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "PID Mediator")

m4df <- broom::tidy(m4.4) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "No Mediators")
  

models <- rbind(m4df, m1df, m2df, m3df) 


 
g4 <- dwplot(models, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ),  dot_args = list(aes(shape = model))) %>% # plot line at zero _behind_coefs
    relabel_predictors(
        c(  black_rTRUE = "Black",
            latino_rTRUE = "Latino",
            asian_rTRUE = "Asian"
        )) + # plot line at zero _behind_ coefs
    theme_bw(base_size = 11) + xlab("Coefficient Estimate") + ylab("") +
    labs(subtitle = "7.4 Democratic Senate") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    scale_colour_grey(
        start = .1,
        end = .7
    ) +
    scale_shape_discrete(
        name = "Model",
    ) +
    guides(
        shape = guide_legend(""), 
        colour = guide_legend("")) +
        theme(legend.direction="horizontal") +
  scale_x_continuous(limits = c(-0.18, 0.14))

g4 <- g4 + theme(legend.position="none") 



lay = rbind(
            c(2,2,2,2,2,2,2,2,2),
            c(2,2,2,2,2,2,2,2,2),
            c(2,2,2,2,2,2,2,2,2),
            c(2,2,2,2,2,2,2,2,2),
            c(3,3,3,3,3,3,3,3,3),
            c(3,3,3,3,3,3,3,3,3),
            c(3,3,3,3,3,3,3,3,3),
            c(3,3,3,3,3,3,3,3,3),
            c(4,4,4,4,4,4,4,4,4),
            c(4,4,4,4,4,4,4,4,4),
            c(4,4,4,4,4,4,4,4,4),
            c(4,4,4,4,4,4,4,4,4),
            c(5,5,5,5,5,5,5,5,5),
            c(5,5,5,5,5,5,5,5,5),
            c(5,5,5,5,5,5,5,5,5),
            c(5,5,5,5,5,5,5,5,5),
            c(NA,1,1,1,1,1,1,1,1))

grid.arrange(mylegend,g1,g2,g3,g4, layout_matrix=lay)
```


# SI

## SI-C

### Reference

```{r}
m1 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           |  as.factor(year_vote) | 0 |  year_vote, data = policy, weights = policy$weight)

m2 <- felm(pref_outcome ~ black_r*reppres + latino_r*reppres + asian_r*reppres + other_r*reppres
           |  as.factor(year_vote) | 0 |  year_vote, data = policy, weights = policy$weight)

m3 <- felm(pref_outcome ~ black_r*rephouse + latino_r*rephouse + asian_r*rephouse + other_r*rephouse 
           | as.factor(year_vote) | 0 |  year_vote,  data = policy, weights = policy$weight)

m4 <- felm(pref_outcome ~ black_r*repsenate + latino_r*repsenate + asian_r*repsenate + other_r*repsenate
           |  as.factor(year_vote) | 0 |  year_vote,  data = policy, weights = policy$weight)
```

### Don't Know Analysis

```{r}
dk <- read.csv("Intermediate Data/dontknow_or_skip_min_by_year.csv")
```

```{r}
m1.dk <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           |  as.factor(year_vote) | 0 |  year_vote, data = subset(policy, year_vote %in% dk$year_vote), weights = subset(policy, year_vote %in% dk$year_vote)$weight)

m2.dk <- felm(pref_outcome ~ black_r*reppres + latino_r*reppres + asian_r*reppres + other_r*reppres
           |  as.factor(year_vote) | 0 |  year_vote, data = subset(policy, year_vote %in% dk$year_vote), weights = subset(policy, year_vote %in% dk$year_vote)$weight)

m3.dk <- felm(pref_outcome ~ black_r*rephouse + latino_r*rephouse + asian_r*rephouse + other_r*rephouse 
           | as.factor(year_vote) | 0 |   year_vote, data = subset(policy, year_vote %in% dk$year_vote), weights = subset(policy, year_vote %in% dk$year_vote)$weight)

m4.dk <- felm(pref_outcome ~ black_r*repsenate + latino_r*repsenate + asian_r*repsenate + other_r*repsenate
           |  as.factor(year_vote) | 0 |   year_vote, data = subset(policy, year_vote %in% dk$year_vote), weights = subset(policy, year_vote %in% dk$year_vote)$weight)
```

```{r}
stargazer(m1.dk, 
          title = "SI-C3.1",
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law")
)
```

```{r}
stargazer(m2.dk, m3.dk , m4.dk,
          title = "SI-C3.2",
          #omit= c("vote", "year"), 
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law")
)
```

### Partisan Inflated Analysis

```{r}
psplits <- read.csv("Intermediate Data/senate_party_splits.csv")[,-c(1)] %>%
  pivot_wider(names_from = dem_leg, values_from = perc_yes)

colnames(psplits) <- c("year_vote", "rep_perc_yes", "dem_perc_yes")

psplits <- psplits %>%
  mutate(rep_senate_support = ifelse(rep_perc_yes > .5, 1, 0),
         dem_senate_support = ifelse(dem_perc_yes > .5, 1, 0))

policy <- left_join(policy, psplits)

policy <- policy %>%
  mutate(
    pii_outcome = case_when(
      # Strong Democrats: 50% chance to follow Democratic Senate majority, 50% chance to keep original outcome
      pid7 == 1 ~ ifelse(runif(n()) < 0.50, dem_senate_support, outcome),
      
      # Normal Democrats: 25% chance to follow Democratic Senate majority, 75% chance to keep original outcome
      pid7 == 2 ~ ifelse(runif(n()) < 0.25, dem_senate_support, outcome),
      
      # Weak Democrats: 10% chance to follow Democratic Senate majority, 90% chance to keep original outcome
      pid7 == 3 ~ ifelse(runif(n()) < 0.10, dem_senate_support, outcome),
      
      # Independents: Always keep their original outcome
      pid7 == 4 ~ outcome,
      
      # Weak Republicans: 10% chance to follow Republican Senate majority, 90% chance to keep original outcome
      pid7 == 5 ~ ifelse(runif(n()) < 0.10, rep_senate_support, outcome),
      
      # Normal Republicans: 25% chance to follow Republican Senate majority, 75% chance to keep original outcome
      pid7 == 6 ~ ifelse(runif(n()) < 0.25, rep_senate_support, outcome),
      
      # Strong Republicans: 50% chance to follow Republican Senate majority, 50% chance to keep original outcome
      pid7 == 7 ~ ifelse(runif(n()) < 0.50, rep_senate_support, outcome),
      
      # Default case (if any other pid7 values exist, retain original outcome)
      TRUE ~ outcome
    ),
    pii_pref_outcome = ifelse(pii_outcome == became_law, 1, 0),
    pii_pref_outcome = ifelse(is.na(pii_outcome), pref_outcome, pii_pref_outcome)
  )
```


```{r}
m1.pii <- felm(pii_pref_outcome ~ black_r + latino_r + asian_r + other_r
           |  as.factor(year_vote) | 0 |  year_vote, data = policy, weights = policy$weight)

m2.pii <- felm(pii_pref_outcome ~ black_r*reppres + latino_r*reppres + asian_r*reppres + other_r*reppres
           |  as.factor(year_vote) | 0 |  year_vote, data = policy, weights = policy$weight)

m3.pii <- felm(pii_pref_outcome ~ black_r*rephouse + latino_r*rephouse + asian_r*rephouse + other_r*rephouse 
           | as.factor(year_vote) | 0 |  year_vote,  data = policy)

m4.pii <- felm(pii_pref_outcome ~ black_r*repsenate + latino_r*repsenate + asian_r*repsenate + other_r*repsenate
           |  as.factor(year_vote) | 0 |  year_vote,  data = policy)
```

```{r}
stargazer(m1.pii, 
          title = "SI-C3.3",
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law")
)
```

```{r}
stargazer(m2.pii, m2.pii , m3.pii,
          title = "SI-C3.4",
          #omit= c("vote", "year"), 
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law")
)
```


```{r}
#Plot
m1df <- broom::tidy(m1) %>% 
  mutate(model = "Reference Model") %>%
  subset(!grepl("other",term) & !is.nan(estimate))

m2df <- broom::tidy(m1.dk) %>% 
  mutate(model = "Simpler Questions") %>%
  subset(!grepl("other",term) & !is.nan(estimate))

m3df <- broom::tidy(m1.pii) %>% 
  mutate(model = "Partisan Inflated Simulation") %>%
  subset(!grepl("other",term) & !is.nan(estimate))

models1 <- rbind(m1df, m2df, m3df) %>%
  relabel_predictors(black_rTRUE = "Black Respondent",
                     latino_rTRUE = "Latino Respondent",
                     asian_rTRUE = "Asian Respondent")


dwplot(models1, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ), dot_args = list(aes(shape = model))) +
    theme_bw(base_size = 11) + xlab("") + ylab("") +

  labs(x="Coefficient Estimate", caption = "Notes:\n
  1) White Respondents are the reference category (White average=0.495).\n
  2) Individuals with other racial self-categorizations are excluded from the figure.\n     
  3) All models include policy question fixed effects and standard errors clustered at that level.") +
    scale_x_continuous(limits = c(-0.1,0.1)) +
  (theme_bw() %+replace% theme(plot.caption = element_custom())) +    
  theme(legend.position = "bottom",
        plot.title = element_text(face = "bold")) +
  scale_color_grey() +
    annotate(geom = "text", x = 0.005, y= 2, label = "White Respondent", color = "grey40",
             angle = 90, size = 3.5) 
```

```{r}
#Plot
m1df <- broom::tidy(m2) %>% 
  mutate(model = "Reference Model") %>%
  subset(!grepl("other",term) & !is.nan(estimate))

m2df <- broom::tidy(m2.dk) %>% 
  mutate(model = "Simpler Questions") %>%
  subset(!grepl("other",term) & !is.nan(estimate))

m3df <- broom::tidy(m2.pii) %>% 
  mutate(model = "Partisan Inflated Simulation") %>%
  subset(!grepl("other",term) & !is.nan(estimate))

m4df <- broom::tidy(m3) %>% 
  mutate(model = "Reference Model") %>%
  subset(!grepl("other",term) & !is.nan(estimate))

m5df <- broom::tidy(m3.dk) %>% 
  mutate(model = "Simpler Questions") %>%
  subset(!grepl("other",term) & !is.nan(estimate))

m6df <- broom::tidy(m3.pii) %>% 
  mutate(model = "Partisan Inflated Simulation") %>%
  subset(!grepl("other",term) & !is.nan(estimate))

m7df <- broom::tidy(m4) %>% 
  mutate(model = "Reference Model") %>%
  subset(!grepl("other",term) & !is.nan(estimate))

m8df <- broom::tidy(m4.dk) %>% 
  mutate(model = "Simpler Questions") %>%
  subset(!grepl("other",term) & !is.nan(estimate))

m9df <- broom::tidy(m4.pii) %>% 
  mutate(model = "Partisan Inflated Simulation") %>%
  subset(!grepl("other",term) & !is.nan(estimate))



models1 <- rbind(m1df, m2df, m3df) %>%
  relabel_predictors(black_rTRUE = "Black Respondent*Democratic\nControl of Veto Point",
                     latino_rTRUE = "Latino Respondent*Democratic\nControl of Veto Point",
                     asian_rTRUE = "Asian Respondent*Democratic\nControl of Veto Point",
                     "black_rTRUE:reppres" = "Black Respondent*Republican\nControl of Veto Point",
                     "reppres:latino_rTRUE" = "Latino Respondent*Republican\nControl of Veto Point",
                     "reppres:asian_rTRUE" = "Asian Respondent*Republican\nControl of Veto Point")

models2 <- rbind(m4df, m5df, m6df) %>%
  relabel_predictors(black_rTRUE = "Black Respondent*Democratic\nControl of Veto Point",
                     latino_rTRUE = "Latino Respondent*Democratic\nControl of Veto Point",
                     asian_rTRUE = "Asian Respondent*Democratic\nControl of Veto Point",
                     "black_rTRUE:reppres" = "Black Respondent*Republican\nControl of Veto Point",
                     "reppres:latino_rTRUE" = "Latino Respondent*Republican\nControl of Veto Point",
                     "reppres:asian_rTRUE" = "Asian Respondent*Republican\nControl of Veto Point")

models3 <- rbind(m7df, m8df, m9df) %>%
  relabel_predictors(black_rTRUE = "Black Respondent*Democratic\nControl of Veto Point",
                     latino_rTRUE = "Latino Respondent*Democratic\nControl of Veto Point",
                     asian_rTRUE = "Asian Respondent*Democratic\nControl of Veto Point",
                     "black_rTRUE:reppres" = "Black Respondent*Republican\nControl of Veto Point",
                     "reppres:latino_rTRUE" = "Latino Respondent*Republican\nControl of Veto Point",
                     "reppres:asian_rTRUE" = "Asian Respondent*Republican\nControl of Veto Point")

g1 <- dwplot(models1, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ), dot_args = list(aes(shape = model))) +
    theme_bw(base_size = 11) + xlab("") + ylab("") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    labs(subtitle = "President") +
    scale_x_continuous(limits = c(-0.38,0.25)) +
    guides(
        shape = guide_legend(""), 
        colour = guide_legend("")) +
        theme(legend.direction="horizontal", legend.position = "bottom") + scale_color_grey()


g2 <- dwplot(models2, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ), dot_args = list(aes(shape = model))) +
    theme_bw(base_size = 11) + xlab("") + ylab("") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    labs(subtitle = "House") +
   scale_x_continuous(limits = c(-0.38,0.25)) +
    guides(
        shape = guide_legend(""), 
        colour = guide_legend("")) +
        theme(legend.position = "none", axis.text.y = element_blank()) + scale_color_grey()

g3 <- dwplot(models3, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ), dot_args = list(aes(shape = model))) +
    theme_bw(base_size = 11) + xlab("") + ylab("") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    labs(subtitle = "Senate") +
    scale_x_continuous(limits = c(-0.38,0.25)) +
    guides(
        shape = guide_legend(""), 
        colour = guide_legend("")) +
        theme(legend.position = "none", axis.text.y = element_blank()) + scale_color_grey()

g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

mylegend<-g_legend(g1)

g1 <- g1 + theme(legend.position="none")

lay = rbind(c(1,1,2,3),
            c(1,1,2,3),
            c(1,1,2,3),
            c(1,1,2,3),
            c(1,1,2,3),
            c(1,1,2,3),
            c(1,1,2,3),
            c(1,1,2,3),
            c(1,1,2,3),
            c(NA,4,4,4))

p3 <- grid.arrange(g1,g2,g3,mylegend, layout_matrix=lay, bottom = "Coefficient Estimate")

p3
```

## SI-D

```{r}
m1 <- felm(pref_outcome ~ black_r*reppres + latino_r*reppres + asian_r*reppres + other_r*reppres
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, vote_before_cces == 1),
           weights = subset(policy, vote_before_cces == 1)$weight)

m2 <- felm(pref_outcome ~ black_r*rephouse + latino_r*rephouse + asian_r*rephouse + other_r*rephouse
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, vote_before_cces == 1),
           weights = subset(policy, vote_before_cces == 1)$weight)

m3 <- felm(pref_outcome ~ black_r*repsenate + latino_r*repsenate + asian_r*repsenate + other_r*repsenate
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, vote_before_cces == 1),
           weights = subset(policy, vote_before_cces == 1)$weight)
```

```{r}
stargazer(m1, m2 , m3,
          title = "Table SI-D",
          #omit= c("vote", "year"), 
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law"),
          out = "Tables/Table SI-D.doc",
          no.space = T
)
```

```{r}
weighted.mean(subset(policy, white_r ==1 & reppres==1 & vote_before_cces==1)$pref_outcome, subset(policy, white_r ==1 & reppres==1 & vote_before_cces==1)$weight, na.rm=T)
sd(subset(policy, white_r ==1 & reppres==1 & vote_before_cces==1)$pref_outcome)
weighted.mean(subset(policy, white_r ==1 & rephouse==1 & vote_before_cces==1)$pref_outcome, subset(policy, white_r ==1 & rephouse==1 & vote_before_cces==1)$weight, na.rm=T)
sd(subset(policy, white_r ==1 & rephouse==1 & vote_before_cces==1)$pref_outcome, na.rm=T)
weighted.mean(subset(policy, white_r ==1 & repsenate==1 & vote_before_cces==1)$pref_outcome, subset(policy, white_r ==1 & repsenate==1 & vote_before_cces==1)$weight, na.rm=T)
sd(subset(policy, white_r ==1 & repsenate==1 & vote_before_cces==1)$pref_outcome, na.rm=T)


weighted.mean(subset(policy, white_r ==1 & reppres==0 & vote_before_cces==1)$pref_outcome, subset(policy, white_r ==1 & reppres==0 & vote_before_cces==1)$weight, na.rm=T)
sd(subset(policy, white_r ==1 & reppres==0 & vote_before_cces==1)$pref_outcome)
weighted.mean(subset(policy, white_r ==1 & rephouse==0 & vote_before_cces==1)$pref_outcome, subset(policy, white_r ==1 & rephouse==0 & vote_before_cces==1)$weight, na.rm=T)
sd(subset(policy, white_r ==1 & rephouse==0 & vote_before_cces==1)$pref_outcome, na.rm=T)
weighted.mean(subset(policy, white_r ==1 & repsenate==0 & vote_before_cces==1)$pref_outcome, subset(policy, white_r ==1 & repsenate==0 & vote_before_cces==1)$weight, na.rm=T)
sd(subset(policy, white_r ==1 & repsenate==0 & vote_before_cces==1)$pref_outcome, na.rm=T)
```

## SI-H Chamber Models

```{r}
#Race Simple Models with Controls
m1 <- felm(house_outcome ~ black_r + latino_r + asian_r + other_r
           |  as.factor(year_vote) | 0 |  year_vote, data = policy, weights = policy$weight)

m2 <- felm(house_outcome ~ black_r*rephouse + latino_r*rephouse + asian_r*rephouse + other_r*rephouse
           |  as.factor(year_vote) | 0 |  year_vote, data = policy, weights = policy$weight)

m3 <- felm(senate_outcome ~ black_r + latino_r + asian_r + other_r
           |  as.factor(year_vote) | 0 |  year_vote, data = policy, weights = policy$weight)

m4 <- felm(senate_outcome ~ black_r*repsenate + latino_r*repsenate + asian_r*repsenate + other_r*repsenate
           |  as.factor(year_vote) | 0 |  year_vote, data = policy, weights = policy$weight)
```

```{r}
stargazer(m1, m2, m3, m4,
          title = "Table SI-H",
          #omit= c("vote", "year"), 
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law"),
          out = "Tables/Table SI-H.doc"
)
```


## SI-I

```{r}
m1 <- felm(pref_outcome ~ black_r*reppres + latino_r*reppres + asian_r*reppres + other_r*reppres + black_r*rephouse + latino_r*rephouse + asian_r*rephouse + other_r*rephouse + black_r*repsenate + latino_r*repsenate + asian_r*repsenate + other_r*repsenate
           |  as.factor(year_vote) | 0 |  year_vote, data = policy, weights = policy$weight)
```

```{r}
stargazer(m1, 
          title = "Table SI-I",
          #omit= c("vote", "year"), 
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law"),
          out = "Tables/Table SI-I.doc"
)
```

# SI-J

```{r}
m1.1 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 1 & majortopic %in% economics),
           weights = subset(policy, reppres == 1 & majortopic %in% economics)$weight)

m1.2 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 1 & majortopic %in% cultural),
           weights = subset(policy, reppres == 1 & majortopic %in% cultural)$weight)

m1.3 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 1 & majortopic %in% defense),
           weights = subset(policy, reppres == 1 & majortopic %in% defense)$weight)

m1.4 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 1 & majortopic %in% other),
           weights = subset(policy, reppres == 1 & majortopic %in% other)$weight)
```

```{r}
stargazer(m1.1, m1.2, m1.3, m1.4,
          title = "SI-J1",
          #omit= c("vote", "year"), 
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law")
)
```

```{r}
m2.1 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 0 & majortopic %in% economics),
           weights = subset(policy, reppres == 0 & majortopic %in% economics)$weight)

m2.2 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 0 & majortopic %in% cultural),
           weights = subset(policy, reppres == 0 & majortopic %in% cultural)$weight)

m2.3 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 0 & majortopic %in% defense),
           weights = subset(policy, reppres == 0 & majortopic %in% defense)$weight)

m2.4 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 0 & majortopic %in% other),
           weights = subset(policy, reppres == 0 & majortopic %in% other)$weight)
```

```{r}
stargazer(m2.1, m2.2, m2.3, m2.4,
          title = "SI-J2",
          #omit= c("vote", "year"), 
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law")
)
```

```{r}
m3.1 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, repsenate == 1 & majortopic %in% economics),
           weights = subset(policy, repsenate == 1 & majortopic %in% economics)$weight)

m3.2 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, repsenate == 1 & majortopic %in% cultural),
           weights = subset(policy, repsenate == 1 & majortopic %in% cultural)$weight)

m3.3 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, repsenate == 1 & majortopic %in% defense),
           weights = subset(policy, repsenate == 1 & majortopic %in% defense)$weight)

m3.4 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, repsenate == 1 & majortopic %in% other),
           weights = subset(policy, repsenate == 1 & majortopic %in% other)$weight)
```

```{r}
stargazer(m3.1, m3.2, m3.3, m3.4, 
          title = "SI-J3",
          #omit= c("vote", "year"), 
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law")
)
```

```{r}
m4.1 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           |  as.factor(year_vote) | 0 |  year_vote, data = subset(policy, repsenate==0 & majortopic %in% economics), weights = subset(policy, repsenate==0 & majortopic %in% economics)$weight)

m4.2 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           |  as.factor(year_vote) | 0 |  year_vote, data = subset(policy, repsenate==0 & majortopic %in% cultural), weights = subset(policy, repsenate==0 & majortopic %in% cultural)$weight)

m4.3 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           |  as.factor(year_vote) | 0 |  year_vote, data = subset(policy, repsenate==0 & majortopic %in% defense), weights = subset(policy, repsenate==0 & majortopic %in% defense)$weight)

m4.4 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r 
           |  as.factor(year_vote) | 0 |  year_vote, data = subset(policy, repsenate==0 & majortopic %in% other), weights = subset(policy, repsenate==0 & majortopic %in% other)$weight)
```

```{r}
stargazer(m4.1, m4.2, m4.3, m4.4, 
          title = "SI-J4",
          #omit= c("vote", "year"), 
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law")
)
```

```{r fig.height=8.5, fig.width=6.5}

m1df <- broom::tidy(m1.1) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE")) %>% 
  mutate(model = "Economic Policy")
  
m2df <- broom::tidy(m1.2) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "Culture/Identity Policy")

m3df <- broom::tidy(m1.3) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "Defense Policy")

m4df <- broom::tidy(m1.4) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "Government Operations")
  

models <- rbind(m4df, m1df, m2df, m3df) 

 
g1 <- dwplot(models, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ),  dot_args = list(aes(shape = model))) %>% # plot line at zero _behind_coefs
    relabel_predictors(
        c(  black_rTRUE = "Black",
            latino_rTRUE = "Latino",
            asian_rTRUE = "Asian"
        )) + # plot line at zero _behind_ coefs
    theme_bw(base_size = 11) + xlab("") + ylab("") +
    labs(subtitle = "Republican President") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    scale_colour_grey(
        start = .1,
        end = .7
    ) +
    scale_shape_discrete(
        name = "Model",
    ) +
    guides(
        shape = guide_legend("", nrow=2,byrow=TRUE), 
        colour = guide_legend("", nrow=2,byrow=TRUE)) +
        theme(legend.direction="horizontal") +
  scale_x_continuous(limits = c(-0.4, 0.4)) +
     theme(legend.position = "bottom") 

g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

mylegend<-g_legend(g1)

g1 <- g1 + theme(legend.position="none") + labs(x="")


m1df <- broom::tidy(m2.1) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE")) %>% 
  mutate(model = "Demo. Mediators")
  
m2df <- broom::tidy(m2.2) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "Ideology Mediator")

m3df <- broom::tidy(m2.3) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "PID Mediator")

m4df <- broom::tidy(m2.4) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "No Mediators")
  

models <- rbind(m4df, m1df, m2df, m3df) 

 
g2 <- dwplot(models, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ),  dot_args = list(aes(shape = model))) %>% # plot line at zero _behind_coefs
    relabel_predictors(
        c(  black_rTRUE = "Black",
            latino_rTRUE = "Latino",
            asian_rTRUE = "Asian"
        )) + # plot line at zero _behind_ coefs
    theme_bw(base_size = 11) + xlab("") + ylab("") +
    labs(subtitle = "Democratic President") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    scale_colour_grey(
        start = .1,
        end = .7
    ) +
    scale_shape_discrete(
        name = "Model",
    ) +
    guides(
        shape = guide_legend("Model"), 
        colour = guide_legend("Model")) +
        theme(legend.position = c(0.55, 0.01)
    ) + theme(legend.position="none") +
  scale_x_continuous(limits = c(-0.4, 0.4)) 


g2 <- g2 + theme(legend.position="none") + labs(x="")


m1df <- broom::tidy(m3.1) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE")) %>% 
  mutate(model = "Demo. Mediators")
  
m2df <- broom::tidy(m3.2) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "Ideology Mediator")

m3df <- broom::tidy(m3.3) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "PID Mediator")

m4df <- broom::tidy(m3.4) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "No Mediators")
  

models <- rbind(m4df, m1df, m2df, m3df) 

 
g3 <- dwplot(models, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ),  dot_args = list(aes(shape = model))) %>% # plot line at zero _behind_coefs
    relabel_predictors(
        c(  black_rTRUE = "Black",
            latino_rTRUE = "Latino",
            asian_rTRUE = "Asian"
        )) + # plot line at zero _behind_ coefs
    theme_bw(base_size = 11) + xlab("") + ylab("") +
    labs(subtitle = "Republican Senate") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    scale_colour_grey(
        start = .1,
        end = .7
    ) +
    scale_shape_discrete(
        name = "Model",
    ) +
    guides(
        shape = guide_legend(""), 
        colour = guide_legend("")) +
        theme(legend.direction="horizontal") +
  scale_x_continuous(limits = c(-0.4, 0.4)) 

g3 <- g3 + theme(legend.position="none") + labs(x="")


m1df <- broom::tidy(m4.1) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE")) %>% 
  mutate(model = "Demo. Mediators")
  
m2df <- broom::tidy(m4.2) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "Ideology Mediator")

m3df <- broom::tidy(m4.3) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "PID Mediator")

m4df <- broom::tidy(m4.4) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "No Mediators")
  

models <- rbind(m4df, m1df, m2df, m3df) 


 
g4 <- dwplot(models, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ),  dot_args = list(aes(shape = model))) %>% # plot line at zero _behind_coefs
    relabel_predictors(
        c(  black_rTRUE = "Black",
            latino_rTRUE = "Latino",
            asian_rTRUE = "Asian"
        )) + # plot line at zero _behind_ coefs
    theme_bw(base_size = 11) + xlab("Coefficient Estimate") + ylab("") +
    labs(subtitle = "Democratic Senate") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    scale_colour_grey(
        start = .1,
        end = .7
    ) +
    scale_shape_discrete(
        name = "Model",
    ) +
    guides(
        shape = guide_legend(""), 
        colour = guide_legend("")) +
        theme(legend.direction="horizontal") +
  scale_x_continuous(limits = c(-0.4, 0.4))


g4 <- g4 + theme(legend.position="none") 



lay = rbind(
            c(2,2,2,2,2,2,2,2,2),
            c(2,2,2,2,2,2,2,2,2),
            c(2,2,2,2,2,2,2,2,2),
            c(2,2,2,2,2,2,2,2,2),
            c(3,3,3,3,3,3,3,3,3),
            c(3,3,3,3,3,3,3,3,3),
            c(3,3,3,3,3,3,3,3,3),
            c(3,3,3,3,3,3,3,3,3),
            c(4,4,4,4,4,4,4,4,4),
            c(4,4,4,4,4,4,4,4,4),
            c(4,4,4,4,4,4,4,4,4),
            c(4,4,4,4,4,4,4,4,4),
            c(5,5,5,5,5,5,5,5,5),
            c(5,5,5,5,5,5,5,5,5),
            c(5,5,5,5,5,5,5,5,5),
            c(5,5,5,5,5,5,5,5,5),
            c(NA,1,1,1,1,1,1,1,NA),
            c(NA,1,1,1,1,1,1,1,NA))

grid.arrange(mylegend,g1,g2,g3,g4, layout_matrix=lay)
```

## SI-K

```{r}
m1.1 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 1 & year_vote %in% subset(poliss, wb_gap > median(poliss$wb_gap))$year_vote),
           weights = subset(policy, reppres == 1 & year_vote %in% subset(poliss, wb_gap > median(poliss$wb_gap))$year_vote)$weight)

m1.2 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 1 & year_vote %in% subset(poliss, wl_gap > median(poliss$wl_gap))$year_vote),
           weights = subset(policy, reppres == 1 & year_vote %in% subset(poliss, wl_gap > median(poliss$wl_gap))$year_vote)$weight)

m1.3 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 1 & year_vote %in% subset(poliss, wa_gap > median(poliss$wa_gap))$year_vote),
           weights = subset(policy, reppres == 1 & year_vote %in% subset(poliss, wa_gap > median(poliss$wa_gap))$year_vote)$weight)

m1.4 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r + pid7
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 1 & year_vote %in% subset(poliss, wb_gap > median(poliss$wb_gap))$year_vote),
           weights = subset(policy, reppres == 1 & year_vote %in% subset(poliss, wb_gap > median(poliss$wb_gap))$year_vote)$weight)


```

```{r}
stargazer(m1.1, m1.2, m1.3, m1.4,
          title = "SI-K1",
          #omit= c("vote", "year"), 
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law")
)
```

```{r}
m2.1 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 0 & year_vote %in% subset(poliss, wb_gap > median(poliss$wb_gap))$year_vote),
           weights = subset(policy, reppres == 0 & year_vote %in% subset(poliss, wb_gap > median(poliss$wb_gap))$year_vote)$weight)

m2.2 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 0 & year_vote %in% subset(poliss, wl_gap > median(poliss$wl_gap))$year_vote),
           weights = subset(policy, reppres == 0 & year_vote %in% subset(poliss, wl_gap > median(poliss$wl_gap))$year_vote)$weight)

m2.3 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 0 & year_vote %in% subset(poliss, wa_gap > median(poliss$wa_gap))$year_vote),
           weights = subset(policy, reppres == 0 & year_vote %in% subset(poliss, wa_gap > median(poliss$wa_gap))$year_vote)$weight)

m2.4 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r + pid7
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, reppres == 0 & year_vote %in% subset(poliss, wb_gap > median(poliss$wb_gap))$year_vote),
           weights = subset(policy, reppres == 0 & year_vote %in% subset(poliss, wb_gap > median(poliss$wb_gap))$year_vote)$weight)
```

```{r}
stargazer(m2.1, m2.2, m2.3, m2.4,
          title = "SI-K2",
          #omit= c("vote", "year"), 
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law")
)
```

```{r}
m3.1 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, repsenate == 1 & year_vote %in% subset(poliss, wb_gap > median(poliss$wb_gap))$year_vote),
           weights = subset(policy, repsenate == 1 & year_vote %in% subset(poliss, wb_gap > median(poliss$wb_gap))$year_vote)$weight)

m3.2 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, repsenate == 1 & year_vote %in% subset(poliss, wl_gap > median(poliss$wl_gap))$year_vote),
           weights = subset(policy, repsenate == 1 & year_vote %in% subset(poliss, wl_gap > median(poliss$wl_gap))$year_vote)$weight)

m3.3 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, repsenate == 1 & year_vote %in% subset(poliss, wa_gap > median(poliss$wa_gap))$year_vote),
           weights = subset(policy, repsenate == 1 & year_vote %in% subset(poliss, wa_gap > median(poliss$wa_gap))$year_vote)$weight)

m3.4 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r + pid7
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, repsenate == 1 & year_vote %in% subset(poliss, wb_gap > median(poliss$wb_gap))$year_vote),
           weights = subset(policy, repsenate == 1 & year_vote %in% subset(poliss, wb_gap > median(poliss$wb_gap))$year_vote)$weight)
```

```{r}
stargazer(m3.1, m3.2, m3.3, m3.4, 
          title = "SI-K3",
          #omit= c("vote", "year"), 
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law")
)
```

```{r}
m4.1 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, repsenate == 0 & year_vote %in% subset(poliss, wb_gap > median(poliss$wb_gap))$year_vote),
           weights = subset(policy, repsenate == 0 & year_vote %in% subset(poliss, wb_gap > median(poliss$wb_gap))$year_vote)$weight)

m4.2 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, repsenate == 0 & year_vote %in% subset(poliss, wl_gap > median(poliss$wl_gap))$year_vote),
           weights = subset(policy, repsenate == 0 & year_vote %in% subset(poliss, wl_gap > median(poliss$wl_gap))$year_vote)$weight)

m4.3 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, repsenate == 0 & year_vote %in% subset(poliss, wa_gap > median(poliss$wa_gap))$year_vote),
           weights = subset(policy, repsenate == 0 & year_vote %in% subset(poliss, wa_gap > median(poliss$wa_gap))$year_vote)$weight)

m4.4 <- felm(pref_outcome ~ black_r + latino_r + asian_r + other_r + pid7
           | as.factor(year_vote) | 0 | year_vote,
           data = subset(policy, repsenate == 0 & year_vote %in% subset(poliss, wb_gap > median(poliss$wb_gap))$year_vote),
           weights = subset(policy, repsenate == 0 & year_vote %in% subset(poliss, wb_gap > median(poliss$wb_gap))$year_vote)$weight)
```

```{r}
stargazer(m4.1, m4.2, m4.3, m4.4, 
          title = "SI-K4",
          #omit= c("vote", "year"), 
          #omit.labels = c("Vote", "Year"), 
          dep.var.labels.include = T , 
          column.sep.width = "1pt", 
          type = "html",  
          font.size = "small", 
          header=F, 
          omit.stat = c("f", "ser"), 
          float = T,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),
          dep.var.labels = c("Law")
)
```

```{r fig.height=8.5, fig.width=6.5}

m1df <- broom::tidy(m1.1) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE")) %>% 
  mutate(model = "Larger Black-White Gap")
  
m2df <- broom::tidy(m1.2) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "Larger Latino-White Gap")

m3df <- broom::tidy(m1.3) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "Larger Asian-White Gap")

m4df <- broom::tidy(m1.4) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "Larger Black-White Gap w/ PID Mediator")
  

models <- rbind(m4df, m1df, m2df, m3df) 

 
g1 <- dwplot(models, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ),  dot_args = list(aes(shape = model))) %>% # plot line at zero _behind_coefs
    relabel_predictors(
        c(  black_rTRUE = "Black",
            latino_rTRUE = "Latino",
            asian_rTRUE = "Asian"
        )) + # plot line at zero _behind_ coefs
    theme_bw(base_size = 11) + xlab("") + ylab("") +
    labs(subtitle = "Republican President") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    scale_colour_grey(
        start = .1,
        end = .7
    ) +
    scale_shape_discrete(
        name = "Model",
    ) +
    guides(
        shape = guide_legend("", nrow=2,byrow=TRUE), 
        colour = guide_legend("", nrow=2,byrow=TRUE)) +
        theme(legend.direction="horizontal") +
  scale_x_continuous(limits = c(-0.3, 0.3)) +
     theme(legend.position = "bottom") 

g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

mylegend<-g_legend(g1)

g1 <- g1 + theme(legend.position="none") + labs(x="")


m1df <- broom::tidy(m2.1) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE")) %>% 
  mutate(model = "Demo. Mediators")
  
m2df <- broom::tidy(m2.2) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "Ideology Mediator")

m3df <- broom::tidy(m2.3) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "PID Mediator")

m4df <- broom::tidy(m2.4) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "No Mediators")
  

models <- rbind(m4df, m1df, m2df, m3df) 

 
g2 <- dwplot(models, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ),  dot_args = list(aes(shape = model))) %>% # plot line at zero _behind_coefs
    relabel_predictors(
        c(  black_rTRUE = "Black",
            latino_rTRUE = "Latino",
            asian_rTRUE = "Asian"
        )) + # plot line at zero _behind_ coefs
    theme_bw(base_size = 11) + xlab("") + ylab("") +
    labs(subtitle = "Democratic President") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    scale_colour_grey(
        start = .1,
        end = .7
    ) +
    scale_shape_discrete(
        name = "Model",
    ) +
    guides(
        shape = guide_legend("Model"), 
        colour = guide_legend("Model")) +
        theme(legend.position = c(0.55, 0.01)
    ) + theme(legend.position="none") +
  scale_x_continuous(limits = c(-0.3, 0.3))

g2 <- g2 + theme(legend.position="none") + labs(x="")


m1df <- broom::tidy(m3.1) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE")) %>% 
  mutate(model = "Demo. Mediators")
  
m2df <- broom::tidy(m3.2) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "Ideology Mediator")

m3df <- broom::tidy(m3.3) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "PID Mediator")

m4df <- broom::tidy(m3.4) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "No Mediators")
  

models <- rbind(m4df, m1df, m2df, m3df) 

 
g3 <- dwplot(models, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ),  dot_args = list(aes(shape = model))) %>% # plot line at zero _behind_coefs
    relabel_predictors(
        c(  black_rTRUE = "Black",
            latino_rTRUE = "Latino",
            asian_rTRUE = "Asian"
        )) + # plot line at zero _behind_ coefs
    theme_bw(base_size = 11) + xlab("") + ylab("") +
    labs(subtitle = "Republican Senate") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    scale_colour_grey(
        start = .1,
        end = .7
    ) +
    scale_shape_discrete(
        name = "Model",
    ) +
    guides(
        shape = guide_legend(""), 
        colour = guide_legend("")) +
        theme(legend.direction="horizontal") +
  scale_x_continuous(limits = c(-0.3, 0.3))

g3 <- g3 + theme(legend.position="none") + labs(x="")


m1df <- broom::tidy(m4.1) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE")) %>% 
  mutate(model = "Demo. Mediators")
  
m2df <- broom::tidy(m4.2) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "Ideology Mediator")

m3df <- broom::tidy(m4.3) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "PID Mediator")

m4df <- broom::tidy(m4.4) %>% 
  filter(term %in% c("black_rTRUE", "latino_rTRUE", "asian_rTRUE"))  %>% 
  mutate(model = "No Mediators")
  

models <- rbind(m4df, m1df, m2df, m3df) 


 
g4 <- dwplot(models, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ),  dot_args = list(aes(shape = model))) %>% # plot line at zero _behind_coefs
    relabel_predictors(
        c(  black_rTRUE = "Black",
            latino_rTRUE = "Latino",
            asian_rTRUE = "Asian"
        )) + # plot line at zero _behind_ coefs
    theme_bw(base_size = 11) + xlab("Coefficient Estimate") + ylab("") +
    labs(subtitle = "Democratic Senate") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    scale_colour_grey(
        start = .1,
        end = .7
    ) +
    scale_shape_discrete(
        name = "Model",
    ) +
    guides(
        shape = guide_legend(""), 
        colour = guide_legend("")) +
        theme(legend.direction="horizontal") +
  scale_x_continuous(limits = c(-0.3, 0.3))

g4 <- g4 + theme(legend.position="none") 



lay = rbind(
            c(2,2,2,2,2,2,2,2,2),
            c(2,2,2,2,2,2,2,2,2),
            c(2,2,2,2,2,2,2,2,2),
            c(2,2,2,2,2,2,2,2,2),
            c(3,3,3,3,3,3,3,3,3),
            c(3,3,3,3,3,3,3,3,3),
            c(3,3,3,3,3,3,3,3,3),
            c(3,3,3,3,3,3,3,3,3),
            c(4,4,4,4,4,4,4,4,4),
            c(4,4,4,4,4,4,4,4,4),
            c(4,4,4,4,4,4,4,4,4),
            c(4,4,4,4,4,4,4,4,4),
            c(5,5,5,5,5,5,5,5,5),
            c(5,5,5,5,5,5,5,5,5),
            c(5,5,5,5,5,5,5,5,5),
            c(5,5,5,5,5,5,5,5,5),
            c(NA,1,1,1,1,1,1,1,NA),
            c(NA,1,1,1,1,1,1,1,NA))

grid.arrange(mylegend,g1,g2,g3,g4, layout_matrix=lay)
```


## SI-L

```{r}
m1 <- felm(pref_outcome ~ black_r*reppres + latino_r*reppres + asian_r*reppres + other_r*reppres
           |  as.factor(year) + as.factor(vote.x) | 0 |  year_vote, data = subset(policy, outcome==1), weights = subset(policy, outcome==1)$weight)

m2 <- felm(pref_outcome ~ black_r*reppres + latino_r*reppres + asian_r*reppres + other_r*reppres
           |  as.factor(year) + as.factor(vote.x) | 0 |  year_vote, data = subset(policy, outcome==0),  weights = subset(policy, outcome==0)$weight)
```

```{r}
#Plot
m1df <- broom::tidy(m1) %>% 
  mutate(model = "Model 1") %>%
  subset(!grepl("other",term) & !is.nan(estimate))

#Plot
m2df <- broom::tidy(m2) %>% 
  mutate(model = "Model 2") %>%
  subset(!grepl("other",term) & !is.nan(estimate))

models1 <- rbind(m1df, m2df)%>%
  relabel_predictors(black_rTRUE = "Black Respondent*Democratic President",
                     latino_rTRUE = "Latino Respondent*Democratic President",
                     asian_rTRUE = "Asian Respondent*Democratic President",
                     "black_rTRUE:reppres" = "Black Respondent*Republican President",
                     "reppres:latino_rTRUE" = "Latino Respondent*Republican President",
                     "reppres:asian_rTRUE" = "Asian Respondent*Republican President")


dwplot(models1, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ), dot_args = list(aes(shape = model))) +
    theme_bw(base_size = 11) + xlab("") + ylab("") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    scale_colour_grey(
        start = .1,
        end = .7,
        breaks = c("Model 1", "Model 2"),
        name = "Model",
        labels = c("Policy Supported by Respondent", "Policy Opposed by Respondent")
    ) +
    scale_shape_discrete(
          breaks = c("Model 1", "Model 2"),
        name = "Model",
        labels = c("Policy Supported by Respondent", "Policy Opposed by Respondent")
    ) +
  labs(x="Coefficient Estimate", caption = "Notes: italic('White Respondents are the reference category. Individuals with other racial')\n      italic('self-categorizations are excluded from the figure. All models include legislation and year')\nitalic('fixed effects. Standard errors are clustered at the legislation -level.')") +
    guides(
        shape = guide_legend(""), 
        colour = guide_legend(""))  +
  (theme_bw() %+replace% theme(plot.caption = element_custom())) +
        theme(legend.position="bottom")
```

# Summary Stats

## In-Text Stats
```{r}
agg_win <- policy %>%
  mutate(pid3 = dplyr::recode(pid7, "1" = "Dem", "2" = "Dem", "3" = "Dem", "4" = "Ind", "5" = "Rep", "6" = "Rep", "5" = "Rep"),
         race2 = dplyr::recode(race, "1"="White", "2"="Black", "3"="Latino", "4"="Asian")) %>%
  group_by(repsenate, race2, pid3) %>%
  subset(!is.na(race2) & !is.na(pid3)) %>%
    summarise(pref_outcome = weighted.mean(pref_outcome, weight, na.rm=T)) %>%
  pivot_wider(names_from = "race2", values_from = "pref_outcome")

agg_win

agg_win <- policy %>%
  mutate(pid3 = dplyr::recode(pid7, "1" = "Dem", "2" = "Dem", "3" = "Dem", "4" = "Ind", "5" = "Rep", "6" = "Rep", "5" = "Rep"),
         race2 = dplyr::recode(race, "1"="White", "2"="Black", "3"="Latino", "4"="Asian")) %>%
  group_by(repsenate, race2) %>%
  subset(!is.na(race2)) %>%
    summarise(pref_outcome = weighted.mean(pref_outcome, weight, na.rm=T)) %>%
  pivot_wider(names_from = "race2", values_from = "pref_outcome")

agg_win


agg_win <- policy %>%
  mutate(pid3 = dplyr::recode(pid7, "1" = "Dem", "2" = "Dem", "3" = "Dem", "4" = "Ind", "5" = "Rep", "6" = "Rep", "5" = "Rep"),
         race2 = dplyr::recode(race, "1"="White", "2"="Black", "3"="Latino", "4"="Asian")) %>%
  group_by(reppres, race2) %>%
  subset(!is.na(race2)) %>%
    summarise(pref_outcome = weighted.mean(pref_outcome, weight, na.rm=T)) %>%
  pivot_wider(names_from = "race2", values_from = "pref_outcome")

agg_win

agg_gap <- policy %>%
  mutate(
         race2 = dplyr::recode(race, "1"="White", "2"="Black", "3"="Latino", "4"="Asian")) %>%
  group_by(race2, cces_name.x)  %>%
  subset(!is.na(race2)) %>%
  summarise(outcome = weighted.mean(outcome, weight, na.rm=T)) %>%
  pivot_wider(names_from = "race2", values_from = "outcome") %>%
  mutate(wb = White-Black,
            wl = White-Latino,
            wa = White-Asian)

mean(abs(agg_gap$wb))
mean(abs(agg_gap$wl))
mean(abs(agg_gap$wa))

agg_gap <- policy %>%
  mutate(edu2 = dplyr::recode(edu,  "2"="hs", "3"="hs", "5"="col", "6"="col")) %>%
  group_by(edu2, cces_name.x)  %>%
  subset(!is.na(edu2)) %>%
  summarise(outcome = weighted.mean(outcome, weight, na.rm=T)) %>%
  pivot_wider(names_from = "edu2", values_from = "outcome") %>%
  mutate(gap = hs-col)

mean(abs(agg_gap$gap))

agg_gap <- policy %>%
  mutate(income2 = dplyr::recode(edu,  "1"="low", "4"="high")) %>%
  group_by(income2, cces_name.x)  %>%
  subset(!is.na(income2)) %>%
  summarise(outcome = weighted.mean(outcome, weight, na.rm=T)) %>%
  pivot_wider(names_from = "income2", values_from = "outcome") %>%
  mutate(gap = low-high)

mean(abs(agg_gap$gap))

agg_gap <- policy %>%
  mutate(female2 = dplyr::recode(female,  "1"="women", "0"="men")) %>%
  group_by(female2, cces_name.x)  %>%
  subset(!is.na(female2)) %>%
  summarise(outcome = weighted.mean(outcome, weight, na.rm=T)) %>%
  pivot_wider(names_from = "female2", values_from = "outcome") %>%
  mutate(gap = women-men)

mean(abs(agg_gap$gap))
```

```{r}
policy <- policy %>%
  group_by(voteview_id_senate, voteview_id_house) %>%
  summarise(support = weighted.mean(outcome, weight, na.rm=T)) %>%
  mutate(majority_support = ifelse(support > .5, 1 , 0)) %>%
  inner_join(policy)
```

```{r}
win_in_majority <- policy %>%
  filter(race <= 4) %>%
  group_by(race, majority_support, reppres) %>%
  summarise(winrate = weighted.mean(pref_outcome, weight, na.rm=T))

```

## DV Means

```{r}

# Overall
weighted.mean(policy$pref_outcome, policy$weight, na.rm=T)

sd(policy$pref_outcome)


# Policy area
weighted.mean(subset(policy, white_r==1 & majortopic %in% economics)$pref_outcome, subset(policy, white_r==1 & majortopic %in% economics)$weight, na.rm=T)

sd(subset(policy, white_r==1 & majortopic %in% economics)$pref_outcome)

weighted.mean(subset(policy, white_r==1 & majortopic %in% cultural)$pref_outcome, subset(policy, white_r==1 & majortopic %in% cultural)$weight, na.rm=T)

sd(subset(policy, white_r==1 & majortopic %in% cultural)$pref_outcome)

weighted.mean(subset(policy, white_r==1 & majortopic %in% defense)$pref_outcome, subset(policy, white_r==1 & majortopic %in% defense)$weight, na.rm=T)

sd(subset(policy, white_r==1 & majortopic %in% defense)$pref_outcome)

weighted.mean(subset(policy, white_r==1 & majortopic %in% other)$pref_outcome, subset(policy, white_r==1 & majortopic %in% other)$weight, na.rm=T)

sd(subset(policy, white_r==1 & majortopic %in% other)$pref_outcome)


# Gaps
weighted.mean(subset(policy, white_r==1 & year_vote %in% subset(poliss, wb_gap > median(poliss$wb_gap))$year_vote)$pref_outcome, subset(policy, white_r==1 & year_vote %in% subset(poliss, wb_gap > median(poliss$wb_gap))$year_vote)$weight, na.rm=T)

sd(subset(policy, white_r==1 & year_vote %in% subset(poliss, wb_gap > median(poliss$wb_gap))$year_vote)$pref_outcome)

weighted.mean(subset(policy, white_r==1 & year_vote %in% subset(poliss, wl_gap > median(poliss$wl_gap))$year_vote)$pref_outcome, subset(policy, white_r==1 & year_vote %in% subset(poliss, wl_gap >median(poliss$wl_gap))$year_vote)$weight, na.rm=T)

sd(subset(policy, white_r==1 & year_vote %in% subset(poliss, wl_gap > median(poliss$wl_gap))$year_vote)$pref_outcome)

weighted.mean(subset(policy, white_r==1 & year_vote %in% subset(poliss,  wa_gap> median(poliss$wa_gap))$year_vote)$pref_outcome, subset(policy, white_r==1 & year_vote %in% subset(poliss,  wa_gap> median(poliss$wa_gap))$year_vote)$weight, na.rm=T)

sd(subset(policy, white_r==1 & year_vote %in% subset(poliss,  wa_gap> median(poliss$wa_gap))$year_vote)$pref_outcome)

# House control
weighted.mean(subset(policy, white_r==1 & rephouse==0)$pref_outcome, subset(policy, white_r==1 & rephouse==0)$weight, na.rm=T)

weighted.mean(subset(policy, white_r==1 & rephouse==1)$pref_outcome, subset(policy, white_r==1 & rephouse==1)$weight, na.rm=T)

sd(subset(policy, white_r==1 & rephouse==1)$pref_outcome)

sd(subset(policy, white_r==1 & rephouse==0)$pref_outcome)

# Pres control
weighted.mean(subset(policy, white_r==1 & reppres==0)$pref_outcome, subset(policy, white_r==1 & reppres==0)$weight, na.rm=T)

sd(subset(policy, white_r==1 & reppres==0)$pref_outcome)

weighted.mean(subset(policy, white_r==1 & reppres==1)$pref_outcome, subset(policy, white_r==1 & reppres==1)$weight, na.rm=T)

sd(subset(policy, white_r==1 & reppres==1)$pref_outcome)

# Senate control
weighted.mean(subset(policy, white_r==1 & repsenate==0)$pref_outcome, subset(policy, white_r==1 & repsenate==0)$weight, na.rm=T)

sd(subset(policy, white_r==1 & repsenate==0)$pref_outcome)

weighted.mean(subset(policy, white_r==1 & repsenate==1)$pref_outcome, subset(policy, white_r==1 & repsenate==1)$weight, na.rm=T)

sd(subset(policy, white_r==1 & repsenate==1)$pref_outcome)


# Senate chamber
weighted.mean(subset(policy, white_r==1 )$senate_outcome, subset(policy, white_r==1 )$weight, na.rm=T)

sd(subset(policy, white_r==1)$senate_outcome)

weighted.mean(subset(policy, white_r==1 & repsenate==0)$senate_outcome, subset(policy, white_r==1 & repsenate==0)$weight, na.rm=T)

sd(subset(policy, white_r==1 & repsenate==0)$senate_outcome)

weighted.mean(subset(policy, white_r==1 & repsenate==1)$senate_outcome, subset(policy, white_r==1 & repsenate==1)$weight, na.rm=T)

sd(subset(policy, white_r==1 & repsenate==1)$senate_outcome)

# house chamber
weighted.mean(subset(policy, white_r==1 )$house_outcome, subset(policy, white_r==1 )$weight, na.rm=T)

sd(subset(policy, white_r==1)$house_outcome)

weighted.mean(subset(policy, white_r==1 & rephouse==0)$house_outcome, subset(policy, white_r==1 & rephouse==0)$weight, na.rm=T)

sd(subset(policy, white_r==1 & rephouse==0)$house_outcome)

weighted.mean(subset(policy, white_r==1 & rephouse==1)$house_outcome, subset(policy, white_r==1 & rephouse==1)$weight, na.rm=T)

sd(subset(policy, white_r==1 & rephouse==1)$house_outcome)

```

### Opinion table

```{r}
opinion <- policy %>%
  subset(race <= 4) %>%
  mutate(
race = ifelse(race == 1, "White Respondent", 
                       ifelse(race==2, "Black Respondent", 
                              ifelse(race==3, "Latino Respondent", "Asian Respondent")))
         ) %>%
  group_by(race, cces_name.x, cces_exact_wording, year) %>%
  summarize(pref = weighted.mean(outcome, weight, na.rm=T)) 

opinion <- opinion %>%
  subset(cces_name.x %in% c( "Minnimum Wage", "Bank Bailout",  "Extend NAFTA", "Stem Cell Research", "Amendment to Ban Gay Marriage", "Illegal Immigrant Citizenship", "Renew Patriot Act phone survielence", "Iran sanctions act", "Iraq Withdrawal")) %>% 
  mutate(area = recode(cces_name.x,  "Minnimum Wage"="Economic", "Bank Bailout"="Economic", "Extend NAFTA"="Economic", "Stem Cell Research"="Cultural", "Amendment to Ban Gay Marriage"="Cultural", "Illegal Immigrant Citizenship"="Cultural", "Renew Patriot Act phone survielence"="Defense", "Iran sanctions act"="Defense", "Iraq Withdrawal"="Defense"),
         area = factor(area, levels = c("Economic", "Cultural","Defense")), 
         cces_name.x = recode(cces_name.x,  "Minnimum Wage"="Minimum Wage", "Bank Bailout"="Bank Bailout (TARP)", "Extend NAFTA"="Extend NAFTA", "Stem Cell Research"="Stem Cell Research", "Amendment to Ban Gay Marriage"="Ban Gay Marriage", "Illegal Immigrant Citizenship"="Pathway to Citizenship for\nUndocumented Immigrants", "Renew Patriot Act phone survielence"="Renew Phone Surveillance\n(Patriot Act)", "Iran sanctions act"="Iran Sanctions Act", "Iraq Withdrawal"= "Withdraw Troops from Iraq"))

write.csv(opinion, "opinions_race_rnr2.csv")
```

